home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 6 / The Arsenal Files 6 (Arsenal Computer).ISO / os2 / am4pmsrc.zip / AM4PMR.C < prev    next >
Text File  |  1996-01-01  |  7KB  |  274 lines

  1. // Thomas Answering machine for PM
  2.  
  3. // File:          AM4PMR.c
  4. // Description:   Reads data from modem
  5.  
  6. // History
  7. // 930206 TO      Now it exists...
  8.  
  9. #define INCL_BASE
  10. #include <os2.h>
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdarg.h>
  15. #include <process.h>
  16. #include <stddef.h>
  17. #include <stdlib.h>
  18. #include <malloc.h>
  19. #include <math.h>
  20. #include <stdarg.h>
  21.  
  22. #include <fcntl.h>
  23. #include <sys/types.h>
  24. #include <time.h>
  25. #include <sys/stat.h>
  26. #include <io.h> 
  27.  
  28. #include "am4pm.h"
  29.  
  30. #if 0
  31. static char * TknStr(char * szStr, unsigned char tkn)
  32. {
  33.    static PCHAR asctxt[32]=
  34.    {
  35.       "Null",
  36.       "SOH",
  37.       "STX",
  38.       "ETX",
  39.       "EOT",
  40.       "ENQ",
  41.       "ACK",
  42.       "BELL",
  43.       "BS",
  44.       "HT",
  45.       "LF",
  46.       "VT",
  47.       "FF",
  48.       "CR",
  49.       "SO",
  50.       "SI",
  51.       "DLE",
  52.       "DC1",
  53.       "DC2",
  54.       "DC3",
  55.       "DC4",
  56.       "NAC",
  57.       "SYN",
  58.       "ETB",
  59.       "CAN",
  60.       "EM",
  61.       "SUB",
  62.       "ESC",
  63.       "FS",
  64.       "GS",
  65.       "RS",
  66.       "US"
  67.    };
  68.    if (tkn >= 32)
  69.       sprintf(szStr, "%c", tkn);
  70.    else
  71.       sprintf(szStr, "<%s>", asctxt[tkn]);
  72.    return szStr;
  73. }
  74.  
  75.  
  76. ULONG TimeDiff   // Calculates time between time2-time1
  77. (
  78.    ULONG time1,
  79.    ULONG time2
  80. )
  81. {
  82.    return time2-time1;
  83. }
  84.  
  85.  
  86. ULONG GetMyTID(void)
  87. {
  88.    PTIB pTib;
  89.    PPIB pPib;
  90.  
  91.    DosGetInfoBlocks(&pTib, &pPib);
  92.    return pTib->tib_ptib2->tib2_ultid;
  93. }
  94. #endif
  95.  
  96.  
  97. void ReadModem(PVOID lpVar)
  98. {
  99.    static USHORT i;
  100.    static ULONG n, ulLen, ulStartRecTime;
  101.    static CHAR achHayesBuff[MAXHAYESMSG]; // Buffer for DCE answers
  102.    static USHORT ichHayesBuff=0, usFBuffPos=0;
  103.    static ULONG ulCount;
  104.    static UCHAR buff[RECBUFFLEN], achFileB[RECFBUFFLEN];
  105.    static BOOL bLastCharDLE=FALSE;
  106.  
  107.    if (fDebug)
  108.       dprintf("Thread for reading started...\n");
  109.  
  110.    DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0);
  111.  
  112.    for (;;)
  113.    {
  114.       if (DosWaitEventSem(semStopRead, SEM_IMMEDIATE_RETURN)==0)
  115.       {
  116.          if (fDebug)
  117.             dprintf("Reading thread halted. usGlobalState=%u\n", usGlobalState);
  118.          DosResetEventSem(semStopRead, &ulCount);
  119.          DosResetEventSem(semGoOnRead, &ulCount);
  120.          DosPostEventSem(semStopedReading);
  121.          DosWaitEventSem(semGoOnRead, SEM_INDEFINITE_WAIT);
  122.          if (fDebug)
  123.             dprintf("Reading thread goes on... usGlobalState=%u\n", usGlobalState);
  124.          bLastCharDLE=FALSE;
  125.          ichHayesBuff=0;
  126.       }
  127.  
  128.       if (usGlobalState==GS_ENDING) // Check if it's time to die
  129.       {
  130.          if (fDebug)
  131.             dprintf("\nReading thread dying...\n");
  132.          return;
  133.       }
  134.       
  135.       if (DosRead(hCom, buff, RECBUFFLEN, &n) || !n) // Läs in från porten
  136.          continue;
  137.  
  138.       for (i=0; i<n; i++)
  139.       {
  140.          if (usGlobalState==GS_READY || usGlobalState==GS_INITREC || usGlobalState==GS_DONE || usGlobalState==GS_INITPLAY)
  141.          {
  142.             switch (buff[i])
  143.             {
  144.             case '\r':
  145.                break;
  146.  
  147.             case '\n':
  148.                if (ichHayesBuff)
  149.                {
  150.                   achHayesBuff[ichHayesBuff]='\0';
  151.                   if (fDebug)
  152.                      dprintf("DCE: '%s'\n", achHayesBuff);
  153.                   if (usGlobalState==GS_INITREC && strcmp((PCHAR)achHayesBuff, "CONNECT")==0)
  154.                   {
  155.                      usGlobalState=GS_RECORDING;
  156.                      bLastCharDLE=FALSE;
  157.                      ulStartRecTime=GetSysMSecs();
  158.                   }
  159.                   else if (usGlobalState==GS_DONE && strcmp((PCHAR)achHayesBuff, "VCON")==0)
  160.                   {
  161.                      if (fDebug)
  162.                         dprintf("<Ready>\n");
  163.                      usGlobalState=GS_READY;
  164.                   }
  165.                   else if (usGlobalState==GS_INITPLAY)
  166.                   {
  167.                      if (fDebug)
  168.                         dprintf("Play started\n");
  169.                      SendIt(pchSendBuff, ulSendBuffLen);
  170.                      usGlobalState=GS_READY;
  171.                   }
  172.                   QueueData(IM_STRFROMDCE, achHayesBuff, ichHayesBuff+1);
  173.                   ichHayesBuff=0;
  174.                }
  175.                break;
  176.  
  177. #if 0   // XON/XOFF now handled by OS/2 COM driver
  178.             case 17:
  179.                QueueData(IM_XON, NULL, 0l);
  180.                if (fDebug)
  181.                   dprintf("XON\n");
  182.                break;
  183.  
  184.             case 19:
  185.                QueueData(IM_XOFF, NULL, 0l);
  186.                if (fDebug)
  187.                   dprintf("XOFF\n");
  188.                break;
  189. #endif
  190.             case 16: // DLE
  191.                if (!bLastCharDLE)
  192.                {
  193.                   bLastCharDLE=TRUE;
  194.                   break;
  195.                }
  196.                else
  197.                   bLastCharDLE=FALSE;
  198.                // Fall through
  199.  
  200.             default:
  201.                if (bLastCharDLE)
  202.                {
  203.  //               ichHayesBuff=0;
  204.                   bLastCharDLE=FALSE;
  205.                   if (buff[i] >= 32 && (szActiveDLECodes[0]=='\0' || strchr((PCHAR)szActiveDLECodes, buff[i]) != NULL))
  206.                   {
  207.                      QueueData(IM_DLEFROMDCE, buff+i, 1);
  208.                      if (fDebug)
  209.                         dprintf("DCE: <DLE> %c\n", buff[i]);
  210.                   }
  211.                   else
  212.                      if (fDebug)
  213.                         dprintf("DCE: <DLE> %c (ignored)\n", buff[i]);
  214.                }
  215.                else if (ichHayesBuff+1 < MAXHAYESMSG)
  216.                {
  217.                   achHayesBuff[ichHayesBuff]=buff[i];
  218.                   ichHayesBuff++;
  219.                }
  220.                break;
  221.             }
  222.          }
  223.          else if (usGlobalState==GS_RECORDING)
  224.          {
  225.             if (bLastCharDLE)
  226.             {
  227.                bLastCharDLE=FALSE;
  228.                if (buff[i]==3) // ETX
  229.                {
  230. //                SetComXONXOFF(FALSE);
  231.                   usGlobalState=GS_DONE;
  232.                   ichHayesBuff=0;
  233.                   ulRecTime+=GetSysMSecs()-ulStartRecTime;
  234.                   usFBuffPos--;  // Remove stored DLE
  235.                }
  236.                else
  237.                {
  238.                   if (buff[i] >= 32)
  239.                   {
  240.                      if (szActiveDLECodes[0]=='\0' || strchr((PCHAR)szActiveDLECodes, buff[i]) != NULL)
  241.                      {
  242.                         QueueData(IM_DLEFROMDCE, buff+i, 1);
  243.                         if (fDebug)
  244.                            dprintf("DCE: <DLE> %c\n", buff[i]);
  245.                      }
  246.                      else
  247.                         if (fDebug)
  248.                            dprintf("DCE: <DLE> %c (ignored)\n", buff[i]);
  249.                      usFBuffPos--;  // Remove stored DLE
  250.                   }
  251.                   else
  252.                   {
  253.                      if (!bDLEConv)
  254.                         achFileB[usFBuffPos++]=buff[i];
  255.                   }
  256.                }
  257.             }
  258.             else
  259.             {
  260.                bLastCharDLE = buff[i] == 16;
  261.                achFileB[usFBuffPos++]=buff[i];
  262.             }
  263.  
  264.             if (usFBuffPos >= sizeof achFileB || (usGlobalState!=GS_RECORDING && usFBuffPos > 0))
  265.             {
  266.                if (hRec != 0)
  267.                   DosWrite(hRec, achFileB, usFBuffPos, &ulLen);
  268.                usFBuffPos=0;
  269.             }
  270.          }
  271.       }
  272.    }
  273. }
  274.